package com.snowflake.ecatalog.service.admin;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import com.snowflake.ecatalog.contant.Status;
import com.snowflake.ecatalog.entity.CompanyCategoryStandard;
import com.snowflake.ecatalog.repository.CompanyCategoryStandardDao;

@Service
@Transactional(readOnly = true)
public class CompanyCategoryStandardService {
	@Autowired
	private CompanyCategoryStandardDao dao;

	public CompanyCategoryStandard getCompanyCategoryStandard(Long id) {
		return dao.findOne(id);
	}

	public List<CompanyCategoryStandard> getAllCompanyCategoryStandard() {
		return (List<CompanyCategoryStandard>) dao.findAll();
	}

	public List<CompanyCategoryStandard> getEnabledCompanyCategoryStandard(List<Long> exludeIdList) {
		List<CompanyCategoryStandard> companyCategoryStandardList = null;
		if (CollectionUtils.isNotEmpty(exludeIdList)) {
			companyCategoryStandardList = dao.findByStatusAndIdNotIn(Status.ENABLE, exludeIdList);
		} else {
			companyCategoryStandardList = dao.findByStatus(Status.ENABLE);
		}
		return companyCategoryStandardList;
	}

	@Transactional(readOnly = false)
	public void enableCompanyCategoryStandard(Long id) {
		dao.changeStatus(Status.ENABLE, id);
	}

	@Transactional(readOnly = false)
	public void disableCompanyCategoryStandard(Long id) {
		dao.changeStatus(Status.DISABLE, id);
	}

	@Transactional(readOnly = false)
	public void saveCompanyCategoryStandard(CompanyCategoryStandard standard) {
		dao.save(standard);
	}

	@Transactional(readOnly = false)
	public void deleteCompanyCategoryStandard(Long id) {
		dao.delete(id);
	}

	public boolean existCategoryInStandard(Long standardId) {
		CompanyCategoryStandard standard = dao.findOne(standardId);
		return !CollectionUtils.isEmpty(standard.getCategorys());
	}

	public Page<CompanyCategoryStandard> getCompanyCategoryStandards(Map<String, Object> filterParams, int pageNumber,
			int pageSize) {
		PageRequest pageRequest = buildPageRequest(pageNumber, pageSize);
		Specification<CompanyCategoryStandard> spec = buildSpecification(filterParams);
		return dao.findAll(spec, pageRequest);
	}

	/**
	 * 创建分页请求.
	 */
	private PageRequest buildPageRequest(int pageNumber, int pagzSize) {
		return new PageRequest(pageNumber - 1, pagzSize);
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<CompanyCategoryStandard> buildSpecification(Map<String, Object> filterParams) {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		Specification<CompanyCategoryStandard> spec = DynamicSpecifications.bySearchFilter(filters.values(),
				CompanyCategoryStandard.class);
		return spec;
	}

}
